package ntd.util.frame.task.handle.data.listen;

import cn.hutool.core.util.StrUtil;
import cn.hutool.db.Entity;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.hutool.log.StaticLog;
import ntd.util.cache.FactoryCache;
import ntd.util.cache.ComCache;
import ntd.util.config.common.enums.EnumDataRule;
import ntd.util.config.common.exception.ComException;
import ntd.util.frame.action.database.tool.ToolDb;
import ntd.util.frame.data.factory.FactoryData;
import ntd.util.frame.data.tool.ToolMysql;

import java.util.List;

public class DataListen {

    private static final ComCache cache = FactoryCache.cache("data-listen-res-cache", 20);

    public static JSONObject getDataByListenId(String dataListenId) {
        return cache.map(dataListenId, () -> {
            ToolMysql toolMysql = new ToolMysql(FactoryData.dataSource());
            // 获取监听器
            Entity listenEntity = toolMysql.findOne("data_listen", "id", dataListenId);

            String extendJson = listenEntity.getStr("extend_json");

            if (StrUtil.isBlank(extendJson) || !JSONUtil.isTypeJSON(extendJson)) {
                return null;
            }

            JSONObject jsonObject = JSONUtil.parseObj(extendJson);

            // 侦听数据源id
            String listenDataSourceId = jsonObject.getStr("listenDataSourceId");
            // 侦听数据表
            String listenTableName = jsonObject.getStr("listenTableName");
            // 侦听列
            String listenDataKey = jsonObject.getStr("listenDataKey");

            Entity where = new Entity(listenTableName);
            // 新版匹配规则
            JSONArray wheres = jsonObject.getJSONArray("wheres");
            if (JSONUtil.isNull(wheres)) {
                throw new ComException("匹配规则不能为空......");
            }
            for (int i = 0; i < wheres.size(); i++) {
                JSONObject wheresJSONObject = wheres.getJSONObject(i);

                // 侦听匹配标识
                String tableWhereKey = wheresJSONObject.getStr("tableWhereKey");
                // 侦听匹配规则
                String tableWhereRule = wheresJSONObject.getStr("tableWhereRule");
                // 侦听匹配值
                String tableWhereVal = wheresJSONObject.getStr("tableWhereVal");

                ToolDb.entityBuild(where, EnumDataRule.val(tableWhereRule), tableWhereKey, tableWhereVal);
            }

            ToolMysql listenToolMysql = new ToolMysql(FactoryData.dataSource(listenDataSourceId));

            List<Entity> entities = listenToolMysql.comQuery(where);
            if (entities.size() < 1) {
                return null;
            }
            return JSONUtil.parseObj(entities.get(0));

//            旧版匹配规则 20230114 注释
//            // 侦听匹配标识
//            String tableWhereKey = jsonObject.getStr("tableWhereKey");
//            // 侦听匹配规则
//            String tableWhereRule = jsonObject.getStr("tableWhereRule");
//            // 侦听匹配值
//            String tableWhereVal = jsonObject.getStr("tableWhereVal");
//
//            Entity where = new Entity(listenTableName);
//            ToolDb.entityBuild(where, EnumDataRule.val(tableWhereRule), tableWhereKey, tableWhereVal);
        });
    }

    public static void main(String[] args) {
        Object dataByListenId = DataListen.getDataByListenId("e870b111567943f3ad8afacf9afac797");

        StaticLog.info(dataByListenId.toString());

    }
}
